package com.lj.mcore.tools;

import com.lj.mcore.utils.CommUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.Map;

public class PromoTools {
    public static String transformToStr(int type,String rules){
        String ruleText="";
        StringBuffer sb = new StringBuffer();
        switch (type){
            case 0:
/*[{over：200,minus：300},{over：500,minus：90},{over：800,minus：200}]*/
                JSONArray jsonArray = JSONArray.fromObject(rules);
                for (Object o:jsonArray){
                    Map map = (Map) o;
                    String over = map.get("over").toString();
                    String minus = map.get("minus").toString();
                    sb.append("满").append(over).append("减").append(minus).append(" ");
                }
                ruleText = sb.toString();
                if (ruleText.length()>0){
                    ruleText=ruleText.substring(0,ruleText.length()-1);
                }
                return ruleText;
            case 1:
                /*[{buyCount：2,rate：8},{over：3,rate：5}]*/
                JSONArray jsonArray1 = JSONArray.fromObject(rules);
                for (Object o:jsonArray1){
                    Map map = (Map) o;
                    String buyCount = map.get("buyCount").toString();
                    String rate = map.get("rate").toString();
                    sb.append(buyCount).append("件").append(rate).append("折").append(" ");
                }
                ruleText = sb.toString();
                if (ruleText.length()>0){
                    ruleText=ruleText.substring(0,ruleText.length()-1);
                }
                return ruleText;
            case 2:
                JSONObject object = JSONObject.fromObject(rules);
                String rate = object.getString("rate");
                if (!CommUtils.null2String(rate).equals("")){
                    ruleText = rate+"折";
                }
                return ruleText;
            default:
                return ruleText;
        }
    }

}
